package com.boot.common.utils;


import com.aliyun.oss.OSSClient;

import org.springframework.stereotype.Service;


import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;


public class AliyunOSSUtil {

    private String endpoint_;
    private String keyid;
    private String keysecret;
    private String bucketname;
    private String filehost;

    public String PDFImageUplod(BufferedImage bufferedImage, String fileName) {
        // 随机名处理
        // 加载配置文件，初始化OSSClient
        String endpoint = endpoint_;
        String accessKeyId = keyid;
        String accessKeySecret = keysecret;
        String bucketName = bucketname;
        String fileHost = filehost;
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // 创建存贮空间
        // ossClient.createBucket(ossConfigure.getBucketName());
        // 创建文件路径
        String fileUrl = fileHost + "/" + ("png/" + UUID.randomUUID().toString().replace("-", "") + "-" + fileName);
        // 上传文件
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            ImageIO.write(bufferedImage, "png", out);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ossClient.putObject(bucketName, fileUrl, new ByteArrayInputStream(out.toByteArray()));
        ossClient.shutdown();
        return fileUrl;
    }
}